﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Add_Customer : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public frm_Add_Customer()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            LoadCountryList();
        }

        /////////////////////////
        /// Load Country List
        /////////////////////////
        private void LoadCountryList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT CountryCode, CountryName FROM [222_Country] ORDER BY CountryName";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["CountryCode"].ToString();
                    item.Text = dataReader["CountryName"].ToString();
                    this.countryID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get country list");
            }
        }
        
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;

            string customer_id = null;
            string quotation = null;
            string name = null;
            string address = null;
            string country = null;
            string telephone = null;
            string fax = null;
            string pic = null;
            string picsex = null;
            string picemail = null;
            string picposition = null;
            string taxcode = null;
            string bankaccount = null;

            ComboboxItem item = new ComboboxItem();

            customer_id = this.customerID.Text;
            if (string.IsNullOrEmpty(customer_id))
            {
                error = "Please enter Customer ID";
            }

            quotation = this.quotationCode.Text;

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.customerName.Text))
                {
                    error = "Please enter Customer Name";
                }
                else
                {
                    name = this.customerName.Text;
                }
            }           
            
            if (string.IsNullOrEmpty(error))
            {
                address = this.addressText.Text;
                if (this.countryID.SelectedIndex >= 0)
                {
                    item = (ComboboxItem)this.countryID.SelectedItem;
                    country = item.Value.ToString();
                }
                telephone = this.telephoneNumber.Text;
                fax = this.faxNumber.Text;
                pic = this.picName.Text;
                picsex = this.picSex.Text;
                picemail = this.picEmail.Text;
                picposition = this.picPosition.Text;
                taxcode = this.taxCode.Text;
                bankaccount = this.bankAccount.Text;
                
                try
                {
                    myConnection.Open();
                    string sql = "INSERT INTO [222_Customer] ([CustomerID], [QuotationCode], [FullName], [Address],"
                        + " [CountryCode], [Telephone], [Fax], [PIC], [PICSex], [PICEmail], [PICPosition], [TaxCode],"
                        + " [BankAccount])"
                        + " VALUES ('" + customer_id + "', '" + quotation + "', '" + name + "', '" + address + "',"
                        + " '" + country + "', '" + telephone + "', '" + fax + "', '" + pic + "', '" + picsex + "',"
                        + " '" + picemail + "', '" + picposition + "', '" + taxcode + "', '" + bankaccount + "')";                    
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);                    
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Customer added success.");
                    clearForm();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot add Customer. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }

        private void clearForm()
        {
            foreach (Control c in this.Controls)
            {
                if (c is DevExpress.XtraEditors.TextEdit)
                {
                    ((DevExpress.XtraEditors.TextEdit)c).Text = null;
                }

                if (c is DevExpress.XtraEditors.ComboBoxEdit)
                {
                    ((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex = -1;
                }

                if (c is System.Windows.Forms.MaskedTextBox)
                {
                    ((System.Windows.Forms.MaskedTextBox)c).Text = null;
                }
            }
        }
    }
}
